<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省份名" prop="provinceName">
        <el-input v-model="queryParams.provinceName" placeholder="请输入省份名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="试验站名" prop="testStationName">
        <el-input v-model="queryParams.testStationName" placeholder="请输入试验站名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="示范县名" prop="demonstrationCountyName">
        <el-input v-model="queryParams.demonstrationCountyName" placeholder="请输入示范县名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品种名" prop="litchiVarieties">
        <el-input v-model="queryParams.litchiVarieties" placeholder="请输入品种名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择年份" clearable>
          <el-option v-for="year in year" :key="year" :label="year" :value="year" />
        </el-select>
      </el-form-item>
      <el-form-item label="排序条件" prop="orderBy">
        <el-select v-model="queryParams.orderBy" placeholder="请选择排序条件" clearable>
          <el-option v-for="option in orderBy" :key="option.index" :label="option.name" :value="option.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:record:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:record:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:record:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:record:export']">导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-info" size="mini" @click="insertCom">插入数据</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="省份名" align="center" prop="provinceName" />
      <el-table-column label="试验站名" align="center" prop="testStationName" />
      <el-table-column label="示范县名" align="center" prop="demonstrationCountyName" />
      <el-table-column label="荔枝品种" align="center" prop="litchiVarieties" />
      <el-table-column label="生产面积(万亩)" align="center" prop="area" />
      <el-table-column label="成花率" align="center" prop="floweringRate" />
      <el-table-column label="产量(万吨)" align="center" prop="production" />
      <el-table-column label="最早成熟期" align="center" prop="theEarliestMaturity" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.theEarliestMaturity, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:record:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">

        <el-form-item label="省份" prop="provinceName">
          <el-select v-model="form.provinceName" placeholder="请选择省份" clearable :style="{ width: '100%' }" @change="getStationList">
            <el-option v-for="(item, index) in provinceList" :key="index" :label="item.stationName" :value="item.stationId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="试验站" prop="testStationName">
          <el-select v-model="form.testStationName" placeholder="请选择试验站" clearable :style="{ width: '100%' }" @change="getCountyList" :disabled="stationSelectorDisabled">
            <el-option v-for="(item, index) in stationList" :key="index" :label="item.stationName" :value="item.stationId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="示范县" prop="demonstrationCountyName">
          <el-select v-model="form.demonstrationCountyName" placeholder="请选择示范县" clearable :style="{ width: '100%' }" :disabled="countySelectorDisabled">
            <el-option v-for="(item, index) in countyList" :key="index" :label="item.stationName" :value="item.stationName" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="荔枝品种" prop="litchiVarieties">
          <el-input v-model="form.litchiVarieties" placeholder="请输入荔枝品种" />
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="成花率" prop="floweringRate">
          <el-input v-model="form.floweringRate" placeholder="请输入成花率" />
        </el-form-item>
        <el-form-item label="现白点日期" prop="whiteDotDate" >
          <el-date-picker clearable v-model="form.whiteDotDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择现白点日期" :style="{ width: '100%' }">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第一次预测产量" prop="firstForecastProduction">
          <el-input v-model="form.firstForecastProduction" placeholder="请输入第一次预测产量" />
        </el-form-item>
        <el-form-item label="第一次预测最早成熟期" prop="firstPredictTheEarliestMaturity">
          <el-date-picker clearable v-model="form.firstPredictTheEarliestMaturity" type="date" value-format="yyyy-MM-dd" placeholder="请选择第一次预测最早成熟期" :style="{ width: '100%' }">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="坐果率" prop="fruitSettingRate">
          <el-input v-model="form.fruitSettingRate" placeholder="请输入坐果率" />
        </el-form-item>
        <el-form-item label="第二次预测产量" prop="secondForecastProduction">
          <el-input v-model="form.secondForecastProduction" placeholder="请输入第二次预测产量" />
        </el-form-item>
        <el-form-item label="第二次预测最早成熟期" prop="secondPredictTheEarliestMaturity">
          <el-date-picker clearable v-model="form.secondPredictTheEarliestMaturity" type="date" value-format="yyyy-MM-dd" placeholder="请选择第二次预测最早成熟期" :style="{ width: '100%' }">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产量" prop="production">
          <el-input v-model="form.production" placeholder="请输入产量" />
        </el-form-item>
        <el-form-item label="最早成熟期" prop="theEarliestMaturity">
          <el-date-picker clearable v-model="form.theEarliestMaturity" type="date" value-format="yyyy-MM-dd" placeholder="请选择最早成熟期" :style="{ width: '100%' }">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最晚成熟期" prop="theLatestMaturity">
          <el-date-picker clearable v-model="form.theLatestMaturity" type="date" value-format="yyyy-MM-dd" placeholder="请选择最晚成熟期" :style="{ width: '100%' }">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/system/record";
// import { listVarity, updateVarity } from "../../../api/system/varity";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示新增/修改弹出层
      open: false,
      // 是否显示柱状图弹出层
      chartShow: false,
      // 试验站表格数据
      stationList: [],
      // 示范县列表
      countyList: [],
      // 省份列表
      provinceList: [],

      stationSelectorDisabled: true,
      countySelectorDisabled: true,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: 0,
        provinceName: null,
        testStationName: null,
        demonstrationCountyName: null,
        litchiVarieties: null,
        year: null,
        orderBy: null,
      },
      // 年份
      year: [
        2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021,
        2022, 2023,
      ],
      // 筛选条件
      orderBy: [
        {
          index: 0,
          name: "生产面积",
          value: "area",
        },
        {
          index: 1,
          name: "产量",
          value: "production",
        },
        {
          index: 2,
          name: "成花率",
          value: "flowering_rate",
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询记录列表 */
    getList() {
      this.loading = true;

      listRecord(this.queryParams).then((response) => {
        
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      listStation(this.queryParams).then((response) => {
        console.log(response);
        console.log(this.queryParams);
        this.provinceList = response.rows;
        this.loading = false;
      });
    },

    /** 查询指定省份的试验站列表 选择省份时触发 */
    getStationList() {
      this.form.testStationName = this.form.demonstrationCountyName = null;
      this.loading = false;
      this.queryParams.parentId = this.form.provinceName;
      listStation(this.queryParams).then((response) => {
        console.log(response);
        this.stationList = response.rows;
        this.loading = false;
      });
    },

    /** 查询指定试验站的示范县列表 选择试验站时触发 */
    getCountyList() {
      this.form.demonstrationCountyName = null;
      this.loading = false;
      this.queryParams.parentId = this.form.testStationName;
      listStation(this.queryParams).then((response) => {
        console.log(response);
        this.countyList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        provinceName: null,
        testStationName: null,
        demonstrationCountyName: null,
        litchiVarieties: null,
        year: null,
        area: null,
        firstFillingDate: null,
        floweringRate: null,
        whiteDotDate: null,
        firstForecastProduction: null,
        firstPredictTheEarliestMaturity: null,
        secondFillingDate: null,
        fruitSettingRate: null,
        secondForecastProduction: null,
        secondPredictTheEarliestMaturity: null,
        thirdFillingDate: null,
        production: null,
        theEarliestMaturity: null,
        theLatestMaturity: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.recordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids;
      getRecord(recordId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      var _this = this;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.recordId != null) {
            updateRecord(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            var result = this.provinceList.find(function (item, index, arr) {
              return item.stationId == _this.form.provinceName;
            });
            this.form.provinceName = result.stationName;

            var result2 = this.stationList.find(function (item, index, arr) {
              return item.stationId == _this.form.testStationName;
            });
            this.form.testStationName = result2.stationName;
            addRecord(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.provinceList = this.stationList = this.countyList = [];
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal
        .confirm('是否确认删除记录编号为"' + recordIds + '"的数据项？')
        .then(function () {
          return delRecord(recordIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/record/export",
        {
          ...this.queryParams,
        },
        `record_${new Date().getTime()}.xlsx`
      );
    },
    /** 插入数据操作 */
    // insertCom() {
    //   this.loading = true;
    //   var varityArr = [];
    //   // 生产面积
    //   var area = 0;
    //   // 产量
    //   var production = 0;
    //   // 成花率
    //   var floweringRate = 0;
    //   var i = 0;
    //   listVarity({ pageSize: 100 }).then(res => {
    //     varityArr = res.rows;
    //   })
    //   listRecord(this.queryParams).then(response => {
    //     varityArr.forEach(varity => {
    //       response.rows.forEach(record => {
    //         if (record.litchiVarieties.search(varity.varityName) != -1) {
    //           area = area + record.area;
    //           production = production + record.production;
    //           floweringRate = floweringRate + record.floweringRate;
    //           i = i + 1;
    //         }
    //       });
    //       if (this.queryParams.year == 2010) {
    //         varity.production2010 = production.toFixed(3);
    //         varity.area2010 = area.toFixed(3);
    //         varity.floweringRate2010 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2011) {
    //         varity.production2011 = production.toFixed(3);
    //         varity.area2011 = area.toFixed(3);
    //         varity.floweringRate2011 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2012) {
    //         varity.production2012 = production.toFixed(3);
    //         varity.area2012 = area.toFixed(3);
    //         varity.floweringRate2012 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2013) {
    //         varity.production2013 = production.toFixed(3);
    //         varity.area2013 = area.toFixed(3);
    //         varity.floweringRate2013 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2014) {
    //         varity.production2014 = production.toFixed(3);
    //         varity.area2014 = area.toFixed(3);
    //         varity.floweringRate2014 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2015) {
    //         varity.production2015 = production.toFixed(3);
    //         varity.area2015 = area.toFixed(3);
    //         varity.floweringRate2015 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2016) {
    //         varity.production2016 = production.toFixed(3);
    //         varity.area2016 = area.toFixed(3);
    //         varity.floweringRate2016 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2017) {
    //         varity.production2017 = production.toFixed(3);
    //         varity.area2017 = area.toFixed(3);
    //         varity.floweringRate2017 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2018) {
    //         varity.production2018 = production.toFixed(3);
    //         varity.area2018 = area.toFixed(3);
    //         varity.floweringRate2018 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2019) {
    //         varity.production2019 = production.toFixed(3);
    //         varity.area2019 = area.toFixed(3);
    //         varity.floweringRate2019 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2020) {
    //         varity.production2020 = production.toFixed(3);
    //         varity.area2020 = area.toFixed(3);
    //         varity.floweringRate2020 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2021) {
    //         varity.production2021 = production.toFixed(3);
    //         varity.area2021 = area.toFixed(3);
    //         varity.floweringRate2021 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       } else if (this.queryParams.year == 2022) {
    //         varity.production2022 = production.toFixed(3);
    //         varity.area2022 = area.toFixed(3);
    //         varity.floweringRate2022 = (floweringRate / i).toFixed(3);
    //         updateVarity(varity).then(res => {
    //           this.$modal.msgSuccess('修改成功');
    //         })
    //         production = 0;
    //         area = 0;
    //         floweringRate = 0;
    //         i = 0;
    //       }
    //     })
    //     this.loading = false;
    //   });
    // }
  }
};
</script>
